var globalPermission = {}; 
$(function(){
	 
        //index导航数据
        var navData = [],
        	$firstNav = $('#first-nav'),
        	$secNav = $('#left-secondnav'),
        	$thirdNav = $('#left-thirdnav');
        
        bindEvent();
        initNav();
        
        function bindEvent() {
        	$("#right").preloader();
        	
        	
        	$firstNav.on('click', 'li', function() {
        		var $this = $(this),
        			fIndex = Number($this.attr('data-f-index'));
        		
        		buildSecNav(navData[fIndex]);
        		$('#left-secondnav').find('li:first').click();
        	});
        	
        	
        	// 二级导航
        	$secNav.on('click', 'li', function() {
        		var $this = $(this),
        			fIndex = Number($this.attr('data-f-index')),
        			sIndex = Number($this.attr('data-s-index'));
        		$secNav.find('li.selected').removeClass('selected');
        		$this.addClass('selected');
        		buildThirdNav(navData[fIndex].children[sIndex]);
        		// $thirdNav.animate({left: '50px'}, 350);
        		// $("#right").animate({left: '220px'}, 350);
        	});
        	
        	// 三级导航
        	$thirdNav.on('click', 'li a', function() {
        		var $this = $(this),
        			url = $this.attr('data-url'),
        			resourceId = $this.attr('data-id');
        		
        		$thirdNav.find('li.selected').removeClass('selected');
        		$this.parent().addClass('selected');
        		
        		setTimeout(function () {
        			$("#right").preloader('show');
            		$('#page-frame').attr('src', basePath + url+'?resourceId='+resourceId);
        		},100);
        		
        		
        	});
        	
        	$('#right iframe').load(function() {
        		$("#right").preloader('hide');
        	});
        	
        	$thirdNav.on('click', '#leftmove-btn', function(){
                var moveLeftNum = $thirdNav.position().left;
                // if(moveLeftNum == 50){
                // 	$thirdNav.animate({left: '-120px'}, 350);
                //     $("#left").animate({width: '50px'}, 350);
                //     $("#right").animate({left: '65px'}, 350);
                // } else{
                // 	$thirdNav.animate({left: '50px'}, 350);
                //     $("#left").animate({width: '220px'}, 350);
                //     $("#right").animate({left: '235px'}, 350);
                // }
            });
        	
        }
        
        function initNav() {
     	   $.ajax({
            	url:basePath + '/getUserResource',
                dataType: 'json',
                success: function (data) {
                	if (data.result=='success') {
                		navData = parseTree(data.data);
                		buildFirstNav(navData);
                		$firstNav.find('li:first').click();
                	}
                    
                }
            }); 
        } 
         
         
         
         function parseTree(data) {
         	var fNav,sNav,tNav,foNav,funcArray,func;
         	for (var i = 0, l = data.length; i < l; i++ ) {
         		fNav = data[i];
         		fNav.index = i;
         		for (var j = 0, m = fNav.children.length; j < m; j++) {
         			sNav = fNav.children[j];
         			sNav.index = j;
         			for (var k = 0, n = sNav.children.length; k < n; k++) {
         				tNav = sNav.children[k];
         				globalPermission[tNav.attributes.permit] = getFunc(tNav);
         			}
         			
         		}
         	}
         	         	
         	return data;
         }
        
        
        
        function buildFirstNav(data) {
        	var domArray = [],
				fnav;
    	
	    	for (var i = 0, l = data.length; i < l; i++) {
	    		fnav = data[i];
	    		domArray.push('<li data-f-index="',fnav.index,'" data-id="',fnav.id,'">',fnav.text,'</li>');
	    	}
	    	$firstNav.html(domArray.join(''));
        }
        
        
        function buildSecNav(firstNav) {
        	var domArray = [],
    			snav;
	    	
	    	for (var i = 0, l = firstNav.children.length; i < l; i++) {
	    		snav = firstNav.children[i];
	    		domArray.push('<li data-f-index="',firstNav.index,'" data-s-index="',snav.index,'" data-id="',snav.id,'">');
	    		domArray.push('<span class="snav-icon"><i class="icon icon-angle-right"></i></span><span class="abc">',snav.text,'</div>');
	    		domArray.push('<span class="snav-text">',snav.text,'</span></li>');
	    	}
	    	$secNav.html(domArray.join(''));
        }
        
        
        function buildThirdNav(secNav) {
        	var domArray = [],
        		tnav;
        	domArray.push('<div><span id="snav-name">',secNav.text,'</span>','<div id="leftmove-btn"><i class="icon icon-arrow-left"></i></div></div>');
        	
        	for (var i = 0, l = secNav.children.length; i < l; i++) {
        		tnav = secNav.children[i];
        		domArray.push('<li class="tnav-text wave wave-dark"><a href="javascript:;" data-id="',tnav.id,'" data-url="',tnav.attributes.url,'">',tnav.text,'</a></li>');
        	}
        	$thirdNav.html(domArray.join(''));
        }
        
        
        function getFunc(tNav) {
        	var funcArray = [];
			$.each(tNav.children, function(index, item) {
				var func = {};
				func.id = item.id;
				func.sName = item.attributes.sName;
				func.permit = item.attributes.permit;
				func.mark = item.attributes.mark;
				func.url = item.attributes.url;
				funcArray.push(func);
			});
			return funcArray;
        }
        
        

        //高亮三级导航的方法
        function lightnav(obj){
            obj.click(function(){
                var $this = $(this);
                obj.removeClass('selected');
                $this.addClass('selected');
            });
        }
        
        
      //修改密码操作
    	$('#modifyPwd').on('click', function () {
    		$('#modifyPwdWindow').window({
    	        title: '修改密码',
    	        width: 560,
    	        height: 280,
    	        iframe: {
    	            url:  basePath + '/pwdChange'
    	        }
    	    });
    	});
    	
    	//退出系统
        $('#logout').click(function(){
        	$.sdialog({
                type: 'confirm',
                msg: '您确定要退出系统吗？',
                onConfirm: function(result) {
                    if (result) {
                    	location.href = basePath + "/logout";
                    }
                }
            });
        });

    });